package com.moli.iov.down.command.mq.kafka.handle;

import com.moli.iov.down.enums.OperateStatusEnum;
import com.moli.iov.down.model.po.VehicleCommand;
import com.moli.iov.down.service.IVehicleCommandService;
import com.moli.iov.protocol.codec.DataWrapper;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  下发命令，网关响应（已下发成功，终端暂未响应）
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/5 17:33
 */
@Component
public class GatewayHandler {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IVehicleCommandService vehicleCommandService;

    /**
     * 下发命令，网关响应 更新数据库
     * @param vin
     * @param dataWrapper
     */
    public void gatewayHandle(String vin, DataWrapper dataWrapper){

        logger.info("[上行响应报文] [网关响应]（已下发成功，终端暂未响应） handle vin: {}, dataWrapper：{}",
                vin, dataWrapper);

        VehicleCommand vehicleCommand = new VehicleCommand();

        vehicleCommand.setVin(vin);
        vehicleCommand.setResponseNo(Long.parseLong(dataWrapper.getSerialNo()));
        vehicleCommand.setStatus(OperateStatusEnum.ALREADY_SEND.getFlag());

        vehicleCommandService.updateResult(vehicleCommand);

        logger.info("[上行响应报文] [网关响应]（已下发成功，终端暂未响应）  vehicleCommand: {}",
            vehicleCommand);
    }
}
